/**
 * Created by lyf on 2017/6/20.
 */

'use strict'

let node_ssh = require('node-ssh');
const serviceConfig = require('../conf/service.json');
let ssh = new node_ssh();

module.exports = function deployKafka(kafka , zookeeper , tsungMaster) {
    return new Promise (async function(resolve,reject){
        console.log(kafka);
        for ( let i = 0 ; i < kafka.length ; i++){
            await ssh.connect({
                host: kafka[i][1],
                username: serviceConfig.user ,
                password: serviceConfig.password
            });
            const KAFKA_BROKER_ID = (kafka[i][0].split('.'))[3];
            const result = await ssh.execCommand(`docker run -d --privileged  ` +
                        `--name kafka ` +
                        ` -v ~/seckill/kafka/data:/data  ` +
                        ` -v ~/seckill/kafka/logs:/logs  ` +
                        ` -p 9092:9092 -p 7203:7203  ` +
                        ` --env KAFKA_ADVERTISED_HOST_NAME=${kafka[i][0]} ` +
                        ` --env ZOOKEEPER_IP=${zookeeper[0][0]} ` +
                        ` --env KAFKA_DELETE_TOPIC_ENABLE=true  ` +
                        ` --env KAFKA_BROKER_ID=${KAFKA_BROKER_ID} ` +
                        `ches/kafka`
            );

            //result.stdout ? resolve() : reject ();

            console.log(`kafka : ${kafka[i][1]}`);
            console.log(`zookeeper:${zookeeper[0][0]}`);
            console.log("-----------------------------");
            console.log(result.stdout);
            console.log(result.stderr);
            console.log("-----------------------------");
            // start
            await ssh.execCommand (`/root/test/ab-tsung/masterSlaveMode/start_target.sh ${tsungMaster[0][0]}`);
            ssh.dispose();
            resolve();

        }
    })

}